package com.yhuso.test.serializableAndExternalizable;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ehsy_it on 2016/4/1.
 */
public class VerifySerializableArrayList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>(Arrays.asList("a","b","c"));
        serializableObject(list);
        derializableObject();
    }

    public static void serializableObject(Object object) {
        ObjectOutputStream oos = null;
        try {
            System.out.println(object);
            oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
            oos.writeObject(object);
            System.out.println("====");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(oos);
        }
    }

    public static void derializableObject() {
        File file = new File("tempFile");
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            List<String> list = (List<String>) ois.readObject();
            System.out.println(list.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(ois);
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
